package gazzera.swing.download;

import gazzera.gui.messages.*;
import gazzera.swing.*;
import gazzera.swing.model.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;

import javax.swing.*;
import javax.swing.table.*;


public class DownloadList extends JTable  {
	
	private List<Download> list = new ArrayList<Download>();
	private DownloadsTableModel model = new DownloadsTableModel(list);
	private JPopupMenu popupMenu = new JPopupMenu();
	
	private JMenuItem menuCancel;
	private final Gazzera gazzera;
	
    public DownloadList(Gazzera gazzera) {
        super();
        this.gazzera = gazzera;
        setModel(model);
        
        setFillsViewportHeight(true);
        setShowGrid(false);
        
        setDragEnabled(false);
        setIntercellSpacing(new Dimension(0,0));
        setCellSelectionEnabled(false);
        setColumnSelectionAllowed(false);
        setRowSelectionAllowed(true);
        setRowHeight(31);
        
        TableColumn col = getColumnModel().getColumn(0); 
        col.setCellRenderer(new DownloadIconRenderer(false));
        col.setMaxWidth(28);
        col.setMinWidth(28);
        col.setResizable(false);
        col.setHeaderValue("");
        
        col = getColumnModel().getColumn(1);
        col.setCellRenderer(new DownloadNameRenderer(false));
        col.setHeaderValue("Filename");
        col.setPreferredWidth(200);
        
        col = getColumnModel().getColumn(2);
        col.setCellRenderer(new ProgressRenderer());
        col.setHeaderValue("Progress");
        col.setPreferredWidth(200);
        
        col = getColumnModel().getColumn(3);
        col.setCellRenderer(new DownloadSizeRenderer());
        col.setHeaderValue("Size");
        col.setPreferredWidth(40);
        
        col = getColumnModel().getColumn(4);
        col.setCellRenderer(new DownloadSpeedRenderer());
        col.setHeaderValue("Speed");
        col.setPreferredWidth(40);
        
        getTableHeader().setReorderingAllowed(false);
        
        
        buildMenu();

        
        
        addMouseListener(new MouseAdapter(){
        	
            public void mouseClicked(MouseEvent e){
             if (e.getClickCount() == 2){
                System.out.println(" double click" );
                }
             }
            public void mousePressed(MouseEvent e) {
                showPopup(e);
              }
              public void mouseReleased(MouseEvent e) {
                showPopup(e);
              }
              private void showPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                  popupMenu(e.getComponent(), e.getX(), e.getY());
                }
              }

            } );

    }

	private void buildMenu()
	{
		menuCancel = new JMenuItem("Cancel"); 
		
		menuCancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Download x = getSelectedDownload();
				gazzera.gui.pass(new CancelDownload(x.id));
			}
		});

        popupMenu.add(menuCancel);
	}

	private Download getSelectedDownload()
	{
		int n = getSelectedRow();
		return (Download)model.getValueAt(n, 0);
	}
    private void popupMenu(Component comp, int x, int y)
    {
    	if (this.getSelectedRowCount() > 0)
		{
    		
    		// Download c = getSelectedDownload();
    		
    		// check visibility of menu items
    		 
    		popupMenu.show(comp, x, y);
		}
    	
    }
    
    public void updateDownload(Download c)
	{
		
		for (Download cc : list)
		{
			if (cc.id == c.id)
			{
				cc.fileId = c.fileId;
				cc.name = c.name;
				cc.size = c.size;
				cc.downloaded = c.downloaded;
				cc.speed = c.speed;
				model.fireTableRowsUpdated(0, list.size());
				return;
			}
		}
		list.add(c);
		model.fireTableRowsInserted(list.size(), list.size());
		this.repaint();
	}
    public void removeDownload(int id)
	{
		Download c = null;
		for (Download cc : list)
		{
			if (cc.id == id)
			{
				c = cc;
				
			}
		}
		if (c!= null) list.remove(c);
		// model.fireTableRowsDeleted();
		this.repaint();
	}
    
	
}
